package com.college.collegeinfomanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.collegeinfomanage.dto.AcademicProgramsDTO;
import com.college.collegeinfomanage.entity.AcademicPrograms;
import com.college.collegeinfomanage.entity.University;
import com.college.collegeinfomanage.mapper.AcademicProgramsMapper;
import com.college.collegeinfomanage.mapper.UniversityMapper;
import com.college.collegeinfomanage.service.AcademicProgramsService;
import com.college.collegeinfomanage.vo.AcademicProgramsListVO;
import com.college.collegeinfomanage.vo.UniversityListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AcademicProgramsServiceImpl extends ServiceImpl<AcademicProgramsMapper, AcademicPrograms> implements AcademicProgramsService {

    @Resource
    private AcademicProgramsMapper academicProgramsMapper;

    @Resource
    private UniversityMapper universityMapper;

    @Override
    public List<UniversityListVO> universityAll() {
        List<University> universityList = universityMapper.selectList(null);
        List<UniversityListVO> collect = universityList.stream()
                .map(university -> {
                    UniversityListVO universityListVO = new UniversityListVO();
                    universityListVO.setId(university.getId());
                    universityListVO.setName(university.getName());
                    return universityListVO;
                })
                .collect(Collectors.toList());
        return collect;
    }

    @Override
    public  IPage<AcademicProgramsListVO> getPage(AcademicProgramsDTO academicProgramsdto) {
        LambdaQueryWrapper<AcademicPrograms> queryWrapper = new LambdaQueryWrapper<>();
        if (academicProgramsdto.getUniversityId() != null) {
            queryWrapper.eq(AcademicPrograms::getUniversityId, academicProgramsdto.getUniversityId());
        }
        if (academicProgramsdto.getDiscipline() != null && !"".equals(academicProgramsdto.getDiscipline())) {
            queryWrapper.like(AcademicPrograms::getDiscipline, academicProgramsdto.getDiscipline());
        }
        if (academicProgramsdto.getDegree() != null && !"".equals(academicProgramsdto.getDegree())) {
            queryWrapper.like(AcademicPrograms::getDegree, academicProgramsdto.getDegree());
        }
        IPage<AcademicPrograms> page = new Page<>(academicProgramsdto.getPage(), academicProgramsdto.getSize());
        IPage<AcademicPrograms> academicProgramsIPage = academicProgramsMapper.selectPage(page, queryWrapper);
        List<AcademicPrograms> records = academicProgramsIPage.getRecords();
        List<AcademicProgramsListVO> collect = records.stream()
                .map(academicPrograms -> {
                    AcademicProgramsListVO academicProgramsListVO = new AcademicProgramsListVO();
                    academicProgramsListVO.setId(academicPrograms.getId());
                    academicProgramsListVO.setUniversityId(academicPrograms.getUniversityId());
                    academicProgramsListVO.setUniversityName(universityMapper.selectById(academicPrograms.getUniversityId()).getName());
                    academicProgramsListVO.setDiscipline(academicPrograms.getDiscipline());
                    academicProgramsListVO.setMajor(academicPrograms.getMajor());
                    academicProgramsListVO.setDegree(academicPrograms.getDegree());
                    return academicProgramsListVO;
                })
                .collect(Collectors.toList());
        IPage<AcademicProgramsListVO> page1 = new Page<>(academicProgramsdto.getPage(), academicProgramsdto.getSize());
        page1.setTotal(academicProgramsIPage.getTotal());
        page1.setPages(academicProgramsIPage.getPages());
        page1.setCurrent(academicProgramsIPage.getCurrent());
        page1.setSize(academicProgramsIPage.getSize());
        page1.setRecords(collect);
        return page1;
    }

    @Override
    public boolean modify(AcademicPrograms academicPrograms) {
        return updateById(academicPrograms);
    }

}
